﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Drawing;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.SalesOneWeb.UserManagement
{
    public partial class UpdateRole : SOSecurityPage
    {
        Facade.StructureFacade structureFacade = new Facade.StructureFacade();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));

                lblError.ForeColor = Color.Black;
                lblError.Text = "";

                if (!IsPostBack)
                {
                    if (Page.Request.Params["RoleId"] != null)
                    {
                        UserRole_aspnet asp_role = structureFacade.GetAspnetUserRole(new Guid(Page.Request.Params["RoleId"]));
                        txtNewRole.Text = asp_role.RoleName;

                        UserRole role = structureFacade.GetUserRole(asp_role.RoleId);
                        UserType userType = structureFacade.GetUserType(role.UserTypeId);
                        if (role.Status == 1)
                            chkEnabled.Checked = true;

                        SetDataSourceToTypes(structureFacade.GetUserTypes(""), ddlUserType);
                        SetDataSourceToTypes(structureFacade.GetUserTypes(""), ddlNewUserType);

                        ddlNewUserType.SelectedValue = role.UserTypeId.ToString();
                        ddlUserType.SelectedValue = role.UserTypeId.ToString();
                        SetDataSourceToRoles(structureFacade.GetAspnetUserRoleForUserType(Convert.ToInt32(ddlUserType.SelectedValue)));
                        ddlRole.SelectedValue = role.RoleId.ToString();
                    }
                    else
                    {
                        SetDataSourceToTypes(structureFacade.GetUserTypes(""), ddlUserType);
                    }
                }
            }
            catch { MassageSummery("IIS514"); }
        }

        public void SetDataSourceToTypes(List<UserType> userTypes,DropDownList ddl)
        {

            ddl.DataSource = userTypes;
            ddl.DataTextField = "UserTypeName";
            ddl.DataValueField = "UserTypeId";
            ddl.DataBind();


            InsertDDLItems(ddl, "Select...", 0);
            //InsertDDLItems(ddlFrom, "Select All", 1);
        }


        protected void ddlRoleType_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (ddlUserType.SelectedIndex > 0)
                    SetDataSourceToRoles(structureFacade.GetAspnetUserRoleForUserType(Convert.ToInt32(ddlUserType.SelectedValue)));
            }
            catch { MassageSummery("IIS514"); }
        }

        protected void ddlNewUserType_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddlRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (ddlRole.SelectedIndex > 0)
                {
                    UserRole_aspnet asp_role = structureFacade.GetAspnetUserRole(new Guid(ddlRole.SelectedValue));
                    txtNewRole.Text = asp_role.RoleName;

                    UserRole role = structureFacade.GetUserRole(asp_role.RoleId);
                    UserType userType = structureFacade.GetUserType(role.UserTypeId);
                    if (role.Status == 1)
                        chkEnabled.Checked = true;

                    SetDataSourceToTypes(structureFacade.GetUserTypes(""), ddlNewUserType);
                    ddlNewUserType.SelectedValue = role.UserTypeId.ToString();
                }
            }
            catch { MassageSummery("IIS514"); }
        }

        public void SetDataSourceToRoles(List<UserRole_aspnet> userRoles)
        {
            ddlRole.DataSource = userRoles;
            ddlRole.DataTextField = "RoleName";
            ddlRole.DataValueField = "RoleId";
            ddlRole.DataBind();

            InsertDDLItems(ddlRole, "Select...", 0);
            //InsertDDLItems(ddlRoles, "Select All", 1);
        }

        public void InsertDDLItems(DropDownList ddl, string Text, int Position)
        {
            //ListItem item = new ListItem(Text);
            //ddl.Items.Insert(Position, item);
            ddl.Items.Insert(Position, Text);
        }

        protected void btnUpdateRole_Click(object sender, EventArgs e)
        {
            try
            {
                UserRole_aspnet asp_role = structureFacade.GetAspnetUserRole(new Guid(Page.Request.Params["RoleId"]));
                UserRole role = new UserRole();
                role.RoleId = new Guid(ddlRole.SelectedValue);
                role.UserTypeId = Convert.ToInt16(ddlNewUserType.SelectedValue);
                if (chkEnabled.Checked)
                    role.Status = 1;
                else
                    role.Status = 0;

                if (structureFacade.UpdateUserRole(role) == 1)
                {                
                    asp_role.RoleName = txtNewRole.Text;
                    structureFacade.UpdateAspnetUserRole(asp_role); 
                    //MassageSummery("IIS401");
                    Response.Redirect("~/UserManagement/ViewRole.aspx?RoleId=" + role.RoleId.ToString() + "&UserType=" + role.UserTypeId.ToString());
                }
                else
                {
                    MassageSummery("IIS502");
                }
            }
            catch 
            {
                MassageSummery("IIS502");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    string url = Request.ServerVariables["HTTP_REFERER"];
            //    url = Request.UrlReferrer.ToString();
            //    Response.Redirect(url);
            //}
            //catch { }
            Response.Redirect("~/UserManagement/ViewRole.aspx");
        }

        public void SetCredentials(StringCollection credential)
        {
            foreach (string item in credential)
            {  
                if (item.ToLower() == "u")
                    btnUpdateRole.Visible = true;

            }
        }
    }
}
